<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Db;

class Users extends Controller
{
    public function _initialize()
    {
        if(!Session::has('Adminname')){
            $this->error('请登录!','../admin');
        }
        if(Session::get('Adminpower')!=1){
            $this->error('您没有访问权限!');
        }
    }
    public function index()
    {
    	$list = Db::name('admin_user')->order('id','asc')->paginate(10);
    	$page = $list->render();
    	$this->assign('list', $list);
		$this->assign('page', $page);
        return $this->fetch();
    }

    //新增用户
    public function add()
    {
        $user = model('User');
        $param = request()->param();
        // return json(['data'=>$param]);
        $data = $user->createuser($param);
        if($data){
            $this->success('创建成功!');
            // return json(['message'=>'创建成功!']);
        }
        return json(['message'=>$user->getError()],400);
    }
    //删除用户
    public function delete()
    {
    	$users = model('User');
    	$param = request()->get('id');
    	$data = $users -> deleteuser($param);
    	if($data){
    		return json(['message'=>'删除成功!']);
    	}
    	return json(['message'=>$users->getError()]);
    }

    //更新页面
    public function updatedetail()
    {
        $users = model('User');
        $param = request()->get('id');
        $data = $users::get($param);
        $this->assign('list',$data);
        return $this->fetch('detail/users');
    }

    //更新
    public function update()
    {
    	$users = model('User');
    	$param = request()->param();
        $param['password'] = pwd_md5($param['password']);
        // return json(['data'=>$param]);
    	$data = $users -> updateuser($param);
    	if($data){
            $this->success('更新成功!');
            // return json(['message'=>'更新成功!']);
        }
        return json(['message'=>$users->getError()]);
    }

    //禁用
    public function cancel()
    {
    	$users = model('User');
    	$param = request()->get('id');
    	$data=$users->canceluser($param);
    	if($data){
    		return json(['message'=>'取消发布成功!']);
    	}
    	return json(['message'=>$users->getError()]);
    }

    //激活
    public function fabu()
    {
    	$users = model('User');
    	$param = request()->get('id');
    	$data=$users->fabuuser($param);
    	if($data){
    		return json(['message'=>'发布成功!']);
    	}
    	return json(['message'=>$users->getError()]);
    }
}
